package com.gxs.ea.fileupload.beans.utils;

import java.util.Iterator;
import java.util.Locale;
import java.util.logging.Logger;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

@SuppressWarnings("serial")
public class LifeCycleListener implements PhaseListener {


  public void afterPhase(PhaseEvent event) {
    if (event.getPhaseId() == PhaseId.RESTORE_VIEW) {

      // let's try first to get the language from the request parameter  
      // and if it is a supported language then put it to session
      String reqParamLang = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("lang");

      if (reqParamLang != null) {
        Iterator<Locale> localeIterator = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
        while (localeIterator.hasNext()) {
          Locale locale = localeIterator.next();
          if (reqParamLang.equals(locale.toString())) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("lang", reqParamLang);
            Logger.getLogger(this.getClass().getName()).info("will change locale to " + locale);
          }
        }
      } 

      // get the language from the session and if available then set the locale 
      String sessionLang = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("lang");

      if (sessionLang != null) {
          FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(sessionLang));
      }
    }
  }

@Override
public void beforePhase(PhaseEvent arg0) {
	// TODO Auto-generated method stub
	
}

@Override
public PhaseId getPhaseId() {
	// TODO Auto-generated method stub
	return PhaseId.RESTORE_VIEW;
}
}